package sceneInfo;

import java.util.ArrayList;
import java.util.Iterator;
import java.io.Serializable;

public class ObjectHistogram implements Serializable {

	public static final long serialVersionUID = 2L;
	
	//type of objects counted in the histogram
	String type; // not necessary
	// size = number of objects
	private int Size=0;
	FuzzyFramework framework; //reference to the fuzzy framework with its parameters
	// size along the 'distance' dimension
	//int SizeDist=0; // not used****
	private int[] histogram; // histogram of fuzzy number of objects in each part of the visible field
					// partitioned by angle and distance
					// cells of the histogram are numbered from left to right, and near to far
	
	//default useless constructor
	public ObjectHistogram() {
	type = "none";	
	}
	
	/*constructor which allow to create a histogram using a number of bins for angle and distance
	 * these parameters will be computed from the fuzzy scene library
	 * they are necessary to initialize the size of the histogram... i guess we could do it some other way
	 * */
	public ObjectHistogram(String thetype, ArrayList<ObjectInfo> objinfo, FuzzyFramework fuzzyframe){
	type = thetype;
	framework = fuzzyframe;
	computeHistogram(objinfo);
	}

	/** constructor to build an empty histogram (with only zeros)*/
	public ObjectHistogram(FuzzyFramework fuzzyframe){
		type = "none";
		framework = fuzzyframe;
		int anglebins= framework.alphanumber;
		int distbins = framework.distnumber;
		
		//first create the histogram array
		histogram = new int[anglebins*distbins]; 
		for (int j = 0; j<anglebins*distbins; j++) histogram[j] = 0;
		}

	//returns a specific value from the histogram
	public int getHistogram(int i){
		return histogram[i];
	}
	
	/** gives the number of objects in this histogram*/
	public int getSize(){
		return Size;
	}
	
	// computes a fuzzy histogram representing the set of objects in the arraylist and their locations
	// need to pass the fuzzy scene lib to have access to membership functions
	public void computeHistogram(ArrayList<ObjectInfo> objinfo) {
		
		// get anglebins and distbins from FuzzyScenelib
		int anglebins= framework.alphanumber;
		int distbins = framework.distnumber;
		
		//first create the histogram array
		histogram = new int[anglebins*distbins]; //size of histogram : product of number of bins in each dimension
		
		//iterator to go through the list of objectInfos and compute our histogram
		Iterator<ObjectInfo> theobjects = objinfo.iterator();
		int memb =0;
		boolean exitcondition = false;
		ObjectInfo obj;
		while ( theobjects.hasNext() && !(exitcondition)){
		      obj=theobjects.next();
		      if ((obj.getType()=="ball")||(obj.getType().startsWith("goal"))) { //this is a hack because the balls and goals are duplicated
		    	  exitcondition=true;
		    	  }
		    for (int b =0; b<anglebins*distbins; b++){ //this loop could be optimized
		    	//add the membership of the object
		    	memb = framework.membership(b, obj);
		    	histogram[b] = histogram[b]+ memb;
		    	Size += memb;
		    	
		    }
		    	
		    }
		return;
	}

}
